package com.exp.controller;

import com.common.controller.BaseController;
import com.exp.model.MyFood;
import com.exp.service.MyFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.nio.file.WatchService;
import java.util.List;
import java.util.Map;

/**
 * Created by guojianyu on 2016/3/30.
 */
@Controller
@RequestMapping("/myFood")
public class MyFoodController extends BaseController {
    @Autowired
    private MyFoodService myFoodService;
    /*我的商家页面*/
    @RequestMapping("/view")
    public String view() {
        return "myFood/myFoodListManage";
    }
    /*管理员查看商家页面*/
    @RequestMapping("/viewAll")
    public String viewAll() {
        return "myFood/myFoodList";
    }
    /*管理员获取商家All列表*/
    @RequestMapping("/listAll")
    @ResponseBody
    public List<MyFood> list(@RequestParam Map map) {
        List<MyFood> myFoods = myFoodService.getMyFoodListAll(map);
        return myFoods;
    }
   /*获取列表*/
    @RequestMapping("/list")
    @ResponseBody
    public List<MyFood> list(@RequestParam Map map,HttpSession session) {
        String businessId= String.valueOf(getLoginBusinessId(session));
        map.put("businessId", businessId);
        List<MyFood> myFoods = myFoodService.getMyFoodList(map);
        return myFoods;
    }
    /*修改状态 begin*/
    @RequestMapping("/enabled")
    @ResponseBody
    /*不加@RequestBody 是因为在index引入defaultHeader*/
    public boolean enabled(MyFood myFood) {
        if(myFood.getId()!=null){
            myFood.setStatus("enabled");
            myFoodService.enabled(myFood);
        }
        return true;
    }
    /*修改状态 end*/
     /*删除*/
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable Integer id) {
        myFoodService.delete(id);
        return true;
    }
    /*增加修改*/
    @RequestMapping("/edit")
    @ResponseBody
    public boolean edit(MyFood myFood,HttpSession session){
        try {
            if(myFood.getId()!=null){
                myFood.setStatus("disabled");
                myFoodService.update(myFood);
            }else{
                myFood.setBusinessId(getLoginBusiness(session).getId());
                myFood.setName(getLoginBusiness(session).getName());
                myFood.setAddress(getLoginBusiness(session).getAddress());
                myFood.setPhone(getLoginBusiness(session).getPhone());
                myFood.setStatus("disabled");
                myFoodService.add(myFood);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
